﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NUnit.Framework;

namespace Milo.Tests
{
    using Builder;
    using Eg.Domain;

    [TestFixture]
    public class BuilderTests
    {
        [Test]
        public void AddCustomerClass()
        {
            CodeGen cg = new CodeGen();

            cg.AddClass(typeof(Customer));

            Type[] types = cg.GetMappedClasses();

            Assert.AreEqual(typeof(Customer), types[0]);
            Assert.AreEqual(typeof(Person), types[1]);
            Assert.AreEqual(typeof(Address), types[2]);
        }

        [Test]
        public void GenerateSource()
        {
            System.IO.StringWriter writer = new System.IO.StringWriter();

            CodeGen cg = new CodeGen();

            cg.AddClass(typeof(Customer));

            cg.GenerateSource(writer);

            System.Console.Write(writer.ToString());
        }

        [Test]
        public void ProcessDll()
        {
#if DEBUG
            string filename = "..\\..\\..\\Milo.Eg.Domain\\bin\\Debug\\Milo.Eg.Domain.dll";
#else
            string filename = "..\\..\\..\\Milo.Eg.Domain\\bin\\Release\\Milo.Eg.Domain.dll";
#endif

            CodeGen cg = new CodeGen();

            cg.AddDllFilename(filename);

            Type[] types = cg.GetMappedClasses();

			Assert.AreEqual(typeof(Customer).FullName, types[0].FullName);
			Assert.AreEqual(typeof(Person).FullName, types[1].FullName);
			Assert.AreEqual(typeof(Address).FullName, types[2].FullName);
        }

		[Test]
		public void GenerateSourceForProtectedSetterType()
		{
			System.IO.StringWriter writer = new System.IO.StringWriter();

			CodeGen cg = new CodeGen();

			cg.AddClass(typeof(Mock.ProtectedSetterType));

			cg.GenerateSource(writer);

			System.Console.Write(writer.ToString());
		}

		[Test]
		public void GenerateSourceForStringArrayProperty()
		{
			string[] s = new string[4];
			IList<string> l = s;

			System.IO.StringWriter writer = new System.IO.StringWriter();

			CodeGen cg = new CodeGen();

			cg.AddClass(typeof(Mock.HasStringArrayType));

			cg.GenerateSource(writer);

			System.Console.Write(writer.ToString());
		}

		[Test]
		public void ProcessDiscreteDll()
		{
			string filename = "..\\..\\..\\..\\Discrete\\Talk2Reg\\Talk2Reg.Domain\\bin\\Debug\\Talk2Reg.Domain.dll";

			try
			{
				CodeGen cg = new CodeGen();

				cg.AddDllFilename(filename);

				System.IO.StringWriter writer = new System.IO.StringWriter();

				cg.GenerateSource(writer);

				System.Console.Write(writer.ToString());
			}
			catch (System.Reflection.ReflectionTypeLoadException ex)
			{
				Console.WriteLine(ex.LoaderExceptions.ToString());
				throw;
			}
		}
    }
}
